package com.team8.verify.listener;

import com.team8.verify.utils.MailVerification;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class RegisterVerifyListener {

    @Autowired
    private MailVerification mailVerification;

    @RabbitListener(bindings = @QueueBinding(
                value = @Queue(
                        value = "as.register.verify",
                        durable = "true"
                ),
                exchange = @Exchange(
                        value = "as.verify.exchange",
                        ignoreDeclarationExceptions = "true",
                        type = ExchangeTypes.TOPIC
                ),
                key = {"#.#"}
            )
    )
    public void listenSendEmail(Map<String, String> msg) throws Exception {

        if (msg == null || msg.size() <= 0) {
            // 放弃处理
            return;
        }
        String email = msg.get("email");
        String code = msg.get("code");
        System.out.println(msg);
        mailVerification.sendVerCode(code, email);
    }
}
